package main

import (
	"github.com/frankrap/huobi-api/hbdm"
	"log"
	"math"
	"math/big"
)

// 简单kline 平均线
func (this *Trade) SimpleAverage(data []hbdm.SwapKLine) {
	allAmount := float64(0)
	allTrade := float64(0)

	l := len(data)
	index := 0
	for i := l - 1; i >= 0; i-- {
		v := data[i]
		allTrade += v.TradeTurnover
		allAmount += v.Amount
		index++
		if index == 5 {
			this.Average.FiveAverage = allTrade / allAmount
			log.Println("5个 15min 均线值为: ", this.Average.FiveAverage, "USDT")
		}
		if index == 10 {
			this.Average.TenAverage = allTrade / allAmount
			log.Println("10个 15min 均线值为: ", this.Average.TenAverage, "USDT")
		}
		if index == 30 {
			this.Average.ThirtyAverage = allTrade / allAmount
			log.Println("30个 15min 均线值为: ", this.Average.ThirtyAverage, "USDT")
		}
	}
}

// 加权 平均线
func (this *Trade) JqAverage(data []hbdm.SwapKLine) {
	l := len(data)
	if l < 1 {
		log.Println("kline 数据为空")
		return
	}
	price := big.NewFloat(0)
	qz := big.NewFloat(0)
	maxPrice := 0.00
	minPrice := 0.00
	for i := 0; i < l-1; i++ {
		// 计算出最近的最大价格 最低价格
		if data[i].High > maxPrice {
			maxPrice = data[i].High
		}
		if data[i+1].High > maxPrice {
			maxPrice = data[i+1].High
		}
		if data[i].Low > minPrice {
			minPrice = data[i].Low
		}
		if data[i+1].Low > minPrice {
			minPrice = data[i+1].Low
		}
		// --------------------------
		gap := math.Abs(data[i].Close + data[i+1].Close)
		tmpQz := big.NewFloat(2)
		tmpQz = tmpQz.Mul(tmpQz, big.NewFloat(float64(i+1)))
		zqPrice := new(big.Float).Mul(big.NewFloat(float64(i+1)), big.NewFloat(gap))
		qz = qz.Add(tmpQz, qz)
		price = price.Add(price, zqPrice)
		if i == 3 {
			five := new(big.Float).Quo(price, qz)
			this.Average.FiveAverage, _ = five.Float64()
			log.Println("5个 15min 加权均线值为: ", this.Average.FiveAverage, "USDT")
		}
		if i == 7 {
			ten := new(big.Float).Quo(price, qz)
			this.Average.TenAverage, _ = ten.Float64()
			log.Println("10个 15min 加权均线值为: ", this.Average.TenAverage, "USDT")
		}
		if i == 28 {
			thirty := new(big.Float).Quo(price, qz)
			this.Average.ThirtyAverage, _ = thirty.Float64()
			log.Println("30个 15min 加权均线值为: ", this.Average.ThirtyAverage, "USDT")
		}
		if i == l-2 {
			// 防止顶部下多单
			last := data[i]
			// 最近一条数据
			// 1 大于最近的平均值
			// 2 基本接近最高点
			// 3 开盘价大于收盘价
			// 4 上颈线
			if last.Close > this.Average.FiveAverage &&
				last.Close > this.Average.TenAverage &&
				last.Close > this.Average.ThirtyAverage &&
				math.Abs(last.Close-maxPrice) < math.Abs(last.Close-minPrice) &&
				last.Open > last.Close &&
				math.Abs(last.Close-last.Low)/math.Abs(last.Open-last.Close) >= 2 {
				this.forceSell = 1
			}
			// 最近一条数据
			// 1 小于最近的平均值
			// 2 基本接近最低点
			// 3 开盘价大于收盘价
			// 4 上颈线
			if last.Close < this.Average.FiveAverage &&
				last.Close < this.Average.TenAverage &&
				last.Close < this.Average.ThirtyAverage &&
				math.Abs(last.Close-maxPrice) > math.Abs(last.Close-minPrice) &&
				last.Open > last.Close &&
				math.Abs(last.Close-last.Low)/math.Abs(last.Open-last.Close) >= 2 {
				this.forceBuy = 1
			}
		}
	}
	// 计算三条平均线最小值
	this.MaxAverage = this.Average.FiveAverage
	if this.Average.TenAverage > this.MaxAverage {
		this.MaxAverage = this.Average.TenAverage
	}
	if this.Average.ThirtyAverage > this.MaxAverage {
		this.MaxAverage = this.Average.ThirtyAverage
	}
	this.MinAverage = this.Average.FiveAverage
	if this.Average.TenAverage < this.MinAverage {
		this.MinAverage = this.Average.TenAverage
	}
	if this.Average.ThirtyAverage < this.MinAverage {
		this.MinAverage = this.Average.ThirtyAverage
	}
}
